// JavaScript Document

////////////////////////////////////////////////////////////////////////////////////
//xh
PJS.xh = function(){
};
PJS.xh.prototype.r = function(){
	var req = null;
	try {
		req = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e){
		try {
			req = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (ee){
			req = null;
		}
	}
	if(!req && typeof XMLHttpRequest != 'undefined') req = new XMLHttpRequest();
	return req;
};
//load xml to element
PJS.xh.prototype.b = function(url,params,callback,el,mode,decode){
	var element = $E(el);
	var xhr = this.r();
	if(!xhr){
		return false;
	}
	params = this.parse_params(params);
	if(url.indexOf("?") == -1){
		url += "?"+params;	
	} else {
		url += "&"+params;	
	}
	var str;
	xhr.open("GET",url);
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			if(!mode){
				str = xhr.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue;
				if(!element.innerHTML){
					try {
						str = xhr.responseXML.firstChild.childNodes[1].nodeValue;
					} catch(e){}
				}
			} else if(mode == "TEXT"){
				str = xhr.responseText;
			}
			if(decode ) str = unescape(str);
			if(element.tagName == "INPUT"){
				element.text = str;
			} else {
				element.innerHTML = str;
			}
		}
	};
	xhr.send(null);	
};
//
PJS.xh.prototype.bc = function(params,el,mode,decode){
	var url = document.location.href;
	this.b(url,params,callback,el,mode,decode);
};
//
PJS.xh.prototype.bcm = function(m,p,el,mode,decode){
	var url = document.location.href;
	var params = [];
	params['action'] = m;
	params['params'] = p;
	var callback = null;
	this.b(url,params,callback,el,mode,decode);
};
//encode
PJS.xh.prototype.encode = function(str){
    return encodeURIComponent(str);
};
//parse params
PJS.xh.prototype.parse_params = function(params){
	var pp = "";
	for(var el in params){
		if(typeof(params[el]) == "function" ) continue;
		pp += el+"="+params[el]+"&";
	}
	if(pp != "" ) pp = pp.substring(0,pp.length-1);
	return pp;
};
//parse result
PJS.xh.prototype.parse_result = function(responseType,xhr){
	var r;
	if(responseType == "JSON"){
		r = this.parse_json(xhr.responseText);
		if(!r) r = {};
	} else if(responseType == "TEXT"){
		r = xhr.responseText
	} else if(responseType == "XML"){
		r = xhr.responseXML
	}
	return r;
};
//parse JSON
PJS.xh.prototype.parse_json = function(filter){
    try {
        if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(filter)){
            var j = eval('(' + filter + ')');
            if(typeof filter === "function"){
                function walk(k,v){
                    if(v && typeof v === "object"){
                        for(var i in v){
                            if(v.hasOwnProperty(i)){
                                v[i] = walk(i,v[i]);
                    }}}
                    return filter(k,v);
                }
                j = walk('',j);
            }
            return j;
        }
    } catch (e){
		// Fall through if the regexp test fails.
	}
};
//running handler
PJS.xh.prototype.running = function(){
	
};
//complete handler
PJS.xh.prototype.complete = function(){
	
};
//call
//@public
//@param   {string}    url             
//@param   {mix}       params          
//@param   {Function}  callback        
//@param   {string}    ransferMode     "GET","POST"
//@param   {string}    responseType    "JSON","XML","TEXT"
//@param   {boolean}   asyn     
//@param   {boolean}   quiet        
PJS.xh.prototype.c = function(url,params,callback,transferMode,responseType,asyn,quiet){
	params = this.parse_params(params);	
	transferMode = typeof(transferMode) === "string" && transferMode.toUpperCase() === "GET" ? "GET" : "POST";
	if(transferMode === "GET"){
		var d = new Date();
		url += params ? (url.indexOf("?") === - 1 ? "?" : "&") + params : "";
		url = encodeURI(url) + (url.indexOf("?") === - 1 ? "?" : "&ajax=1&ajaxtime=") + d.getTime() + d.getMilliseconds();
		params = null;
	}
    responseType = typeof(responseType) === "string" && ((responseType = responseType.toUpperCase()) === "JSON" || responseType === "XML") ? responseType : "TEXT";
    asyn = asyn === false ? false : true;
    var xhr = this.r();
	var _obj = this;
	try {
		if(typeof(this.running) === "function" && !quiet){
			this.running();
		}
		xhr.open(transferMode,url,asyn);
		if(transferMode === "POST"){
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		}
		if(asyn){
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4){
					switch(xhr.status){
						case 0:
						case 200:
							if(typeof(this.complete) === "function"){
								this.complete();
							}
							if(callback == "alert"){
								alert(xhr.responseText);
							} else if(typeof(callback) === "function"){
								var r = _obj.parse_result(responseType,xhr);
								//callback.call(r,xhr.responseText);
								//callback(xhr.responseText);
								callback(r);
							}
							break;
						default:
							break;
					}
					r = null;
				}
			};
			if(xhr != null) xhr.send(params);
		} else {
			xhr.send(params);
			if(typeof(this.complete) === "function" && !quiet){
				this.complete();
			}
			if(xhr.status == 200){
				if(typeof(callback) === "function"){
					var r = this.parse_result(responseType,xhr);
					//callback.call(r,xhr.responseText);
					//callback(xhr.responseText);
					callback(r);
					return r;
				} else {
					alert("HTTP error " + xhr.status);
				}
			}
		}
	} catch (ex){
		if(typeof(this.complete) === "function" && !quiet){
			this.complete();
		}
	}
};
//get
PJS.xh.prototype.get = function(url,params,callback){
	if(url == ""){
		url = document.location.href;
	}
	this.c(url,params,callback,"GET","TEXT");
};
PJS.xh.prototype.fetch = function(url,params,callback){
	if(url == ""){
		url = document.location.href;
	}
	this.c(url,params,callback,"GET","TEXT");
};
PJS.xh.prototype.json = function(url,params,callback){
	if(url == ""){
		url = document.location.href;
	}
	this.c(url,params,callback,"GET","JSON");
};
PJS.xh.prototype.xml = function(url,params,callback){
	if(url == ""){
		url = document.location.href;
	}
	this.c(url,params,callback,"GET","XML");
};
//common currnet
PJS.xh.prototype.d = function(url,params,callback){
	if(url == ""){
		url = document.location.href;
	}
	this.c(url,params,callback);
};
//call currnet
PJS.xh.prototype.cd = function(params,callback){
	var url = document.location.href;
	this.c(url,params,callback);
};
//call currnet method
PJS.xh.prototype.cdm = function(m,p,callback){
	var params = [];
	params['action'] = m;
	params['params'] = p;
	this.cd(params,callback);
};
//call currnet method and then bind
PJS.xh.prototype.cdb = function(m,p,el){
	el = $E(el);
	if(!el) return;
	var params = [];
	params['action'] = m;
	params['params'] = p;
	var callback = function(str){
		if(el.tagName == "INPUT"){
			el.value = str;
		} else {
			el.innerHTML = str;
		}
	};
	this.cd(params,callback);
};
//
PJS.xh.prototype.cdba = function(m,el){
	this.cdb(m,"",el);
};
//call back
PJS.xh.prototype.cdcb = function(msg){
	alert(msg);
};
//
var pjs_xh = new PJS.xh();